﻿using Adnc.Shared.Events;
using DotNetCore.CAP;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WYT.Ord.Application.Contracts.Dtos.Order;
using WYT.Ord.Application.Contracts.Services;

namespace WYT.Ord.Application.EventSubscribers
{
    /// <summary>
    /// 
    /// </summary>
    public class CapEventSubscriber : ICapSubscribe
    {
        private readonly IServiceProvider _services;
        private readonly ILogger<CapEventSubscriber> _logger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        /// <param name="logger"></param>
        public CapEventSubscriber(
            IServiceProvider services
            , ILogger<CapEventSubscriber> logger)
        {
            _services = services;
            _logger = logger;
        }

        /// <summary>
        /// 订单商品出库
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(OrderExwarehouseCompleteEvent))]
        public async Task ProcessOrderExwarehouseEvent(OrderExwarehouseCompleteEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IOrderAppService>();
            await appSrv.TasksAfterExwarehouseComplete(data.OrderId);
        }

        /// <summary>
        /// 订单状态变化
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(OrderStateChangeEvent))]
        public async Task ProcessOrderStateChangeEvent(OrderStateChangeEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IOrderAppService>();
            await appSrv.ChangeOrderState(new OrderStateModifyDto() { OrderId = data.OrderId,ChangeTime = data.ChangeTime,ChangeType = data.ChangeType });
        }
    }
}
